package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.MaintenanceFileDao;
import cn.topevery.garden.entity.po.MaintenanceFile;
import cn.topevery.garden.entity.query.MaintenanceFileQuery;
import cn.topevery.garden.entity.vo.MaintenanceFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbCommand;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlDeleteMate;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 保养记录 附件表
 *
 * @author code-generate
 * @date 2022-02-17
 */
@Repository
public class MaintenanceFileDaoImpl extends BaseDaoImpl<MaintenanceFile> implements MaintenanceFileDao {
    @Override
    public <P extends DbCsp> List<MaintenanceFile> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<MaintenanceFileVO> listPage(MaintenanceFileQuery query) {
        String table = "t_maintenance_file tmf";

        String columns = "tmf.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tmf.c_id desc");
        sqlQueryMate
                .where(true, "tmf.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, MaintenanceFileVO.class);
    }

    @Override
    public MaintenanceFile getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<MaintenanceFile> getListByMainId(Integer mainId) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_maintenance_file tmf").setColumns("tmf.*");
        sqlQueryMate.where(true, "tmf.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tmf.c_main_id", ScriptConditionEnum.Eq, mainId);
        return selectList(sqlQueryMate, MaintenanceFile.class);
    }

    @Override
    public void deleteByMainId(Integer mainId) {
        String sql = "delete from t_maintenance_file tmf where c_main_id = " + mainId;
        run(sql, CommandType.Text, DbCommand::executeNonQuery);
    }

    @Override
    public void deleteByMainIds(Integer[] mainIds) {
        SqlDeleteMate sqlDeleteMate = new SqlDeleteMate("t_maintenance_file tmf");
        sqlDeleteMate.where(true, "c_main_id", ScriptConditionEnum.In, mainIds);
        run(sqlDeleteMate.getSql(), CommandType.Text, dbCommand -> {
            sqlDeleteMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
